Android Bitmap : collision Detecting [on hold]

Posted by user2505374 on Game Development See other posts from Game Development or by user2505374
Published on 2013-08-01T10:39:35Z Indexed on 2013/08/02 16:08 UTC
Read the original article Hit count: 301

Filed under:
|

I am writing an Android game right now and I would need some help in the collision of the wall on screen. When I drag the ball in the top and right it able to collide in wall but when I drag it faster it was able to overlap in the wall.

public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
    // if the player moves
    case MotionEvent.ACTION_MOVE: {
        if (playerTouchRect.contains(x, y)) {
            boolean left = false;
            boolean right = false;
            boolean up = false;
            boolean down = false;
            boolean canMove = false;
            boolean foundFinish = false;

            if (x != pLastXPos) {
                if (x < pLastXPos) {
                    left = true;
                } else {
                    right = true;
                }
                pLastXPos = x;
            }
            if (y != pLastYPos) {
                if (y < pLastYPos) {
                    up = true;
                } else {
                    down = true;
                }
                pLastYPos = y;
            }

            plCellRect = getRectFromPos(x, y);
            newplRect.set(playerRect);

            newplRect.left = x - (int) (playerRect.width() / 2);
            newplRect.right = x + (int) (playerRect.width() / 2);
            newplRect.top = y - (int) (playerRect.height() / 2);
            newplRect.bottom = y + (int) (playerRect.height() / 2);
            int currentRow = 0;
            int currentCol = 0;


            currentRow = getRowFromYPos(newplRect.top);
            currentCol = getColFromXPos(newplRect.right);




            if(!canMove){
                canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] == Cell.wall;
                canMove =true;
            }

            finishTest = mapManager.getCurrentTile().pMaze[currentRow][currentCol];
            foundA = finishTest == Cell.valueOf(letterNotGet + "");



            canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] != Cell.wall;
            canMove = (finishTest == Cell.floor || finishTest == Cell.pl) && canMove;


            if (canMove) {
                invalidate();

                setTitle();
            }



            if (foundA) {
                mapManager.getCurrentTile().pMaze[currentRow][currentCol] = Cell.floor;
                //  finishTest

                letterGotten.add(letterNotGet);

                playCurrentLetter();

                /*sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);*/
                foundS = letterNotGet == 's';

                letterNotGet++;

            }if(foundS){
                AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
                builder.setTitle(mainActivity.getText(R.string.finished_title));
                LayoutInflater inflater = mainActivity.getLayoutInflater();
                View view = inflater.inflate(R.layout.finish, null);
                builder.setView(view);
                View closeButton =view.findViewById(R.id.closeGame);
                closeButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View clicked) {
                        if(clicked.getId() == R.id.closeGame) {
                            mainActivity.finish();
                        }
                    }
                });
                AlertDialog finishDialog = builder.create();
                finishDialog.show();
            } 
            else {
                Log.d(TAG, "INFO: updated player position");
                playerRect.set(newplRect);
                setTouchZone();
                updatePlayerCell();
            }



        } // end of (CASE) if playerTouch

        break;
    } // end of (SWITCH) Case motion
    }//end of Switch

    return true;
}//end of TouchEvent

private void finish() {
    // TODO Auto-generated method stub

}

public int getColFromXPos(int xPos) {
    val = xPos / (pvWidth / mapManager.getCurrentTile().pCols);
    if (val == mapManager.getCurrentTile().pCols) {
        val = mapManager.getCurrentTile().pCols - 1;
    }
    return val;
}

/**
 * Given a y pixel position, return the row of the cell it is in This is
 * used when determining the type of adjacent Cells.
 * 
 * @param yPos
 *            y position in pixels
 * @return The cell this position is in
 */
public int getRowFromYPos(int yPos) {
    val = yPos / (pvHeight / mapManager.getCurrentTile().pRows);
    if (val == mapManager.getCurrentTile().pRows) {
        val = mapManager.getCurrentTile().pRows - 1;
    }
    return val;
}

/**
 * When preserving the position we need to know which cell the player is in,
 * so calculate it from the centre on its Rect
 */
public void updatePlayerCell() {
    plCell.x = (playerRect.left + (playerRect.width() / 2))
            / (pvWidth / mapManager.getCurrentTile().pCols);
    plCell.y = (playerRect.top + (playerRect.height() / 2))
            / (pvHeight / mapManager.getCurrentTile().pRows);

    if (mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] == Cell.floor) {
        for (int row = 0; row < mapManager.getCurrentTile().pRows; row++) {
            for (int col = 0; col < mapManager.getCurrentTile().pCols; col++) {
                if (mapManager.getCurrentTile().pMaze[row][col] == Cell.pl) {
                    mapManager.getCurrentTile().pMaze[row][col] = Cell.floor;
                    break;
                }
            }
        }

        mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] = Cell.pl;
    }
}

public Rect getRectFromPos(int x, int y) {
    calcCell.left = ((x / cellWidth) + 0) * cellWidth;
    calcCell.right = calcCell.left + cellWidth;
    calcCell.top = ((y / cellHeight) + 0) * cellHeight;
    calcCell.bottom = calcCell.top + cellHeight;

    Log.d(TAG, "Rect: " + calcCell + " Player: " + playerRect);
    return calcCell;
}

public void setPlayerRect(Rect newplRect) {
    playerRect.set(newplRect);
}

private void setTouchZone() {
    playerTouchRect.set(
            playerRect.left - playerRect.width() / TOUCH_ZONE,

            playerRect.top - playerRect.height() / TOUCH_ZONE,
            playerRect.right + playerRect.width() / TOUCH_ZONE,
            playerRect.bottom + playerRect.height() / TOUCH_ZONE);
}

public Rect getPlayerRect() {
    return playerRect;
}

public Point getPlayerCell() {
    return plCell;
}

public void setPlayerCell(Point cell) {
    plCell = cell;
}

© Game Development or respective owner

Related posts about java

Related posts about android